<?php
session_start();

include 'dbconnect.php';
$cid = $_GET['cid'];

  // Check connection
if (mysqli_connect_errno())
  {
  //echo "alert'Failed to connect to MySQL: ' . mysqli_connect_error()";
  }
$number = $_SESSION['number'];
if ($number[0] == "+")
	$number[0] = "_";
$table = "SELECT * FROM messages".$number." order by timestamp";
$result = mysqli_query($con, $table);
$html = "<div class='woogie-content-center-messages' id='woogie-content-center-messages'>";
$numberSend = "";
	
while($row = mysqli_fetch_array($result))
  {
	if($row['contactId'] == $cid)
	{
		$numberSend = $row['contactNumber'];
		$mil = $row['timestamp'];
		$seconds = $mil / 1000;
		$date = date("D, d M Y, H:i:s", $seconds);
		//left - other person
		if($row['type']==1){
			$html = $html."<div class='woogie-message-left'>".
						"<div class='woogie-bubble-left'>".
							"<div class='woogie-left-delete'>".
								"<img onclick='deleteMessage($row[id])' src='images/delete_red.png'/>".
							"</div>".
							"<div class='woogie-left-text'>".
								/*"<b>".$row['contactNumber']."</b><br/>"*/$row['text'].
							"</div>".
							"<div class='woogie-left-time'>".
								$date.
							"</div>".
						"</div>".
					"</div>";
		}
		//right - me
		else if($row['type']==2){
			$html = $html."<div class='woogie-message-right'>".
						"<div class='woogie-bubble-right'>".
							"<div class='woogie-right-delete'>".
								"<img onclick='deleteMessage($row[id])' src='images/delete_red.png'/>".
							"</div>".
							"<div class='woogie-right-text'>".
								/*"<b>Me</b><br/>"*/$row['text'].
							"</div>".
							"<div class='woogie-right-time'>".
								$date.
							"</div>".
						"</div>".
					"</div>";
		}
	}
  }
  
$table = "SELECT * FROM contacts".$number.";";
$result = mysqli_query($con, $table);
$cname="";
$cemail = "";
$cnumber = "";
		
while($row = mysqli_fetch_array($result))
  {
	if($row['contactId'] == $cid)
	{
		if ($numberSend == ""){
			$numberSend = $row['contactNumber'];
		}
		$cname = $row['contactName'];
		$cemail = $row['contactEmail'];
		$cnumber = $row['contactNumber'];
		break;
	}
  }
  $html = $html."</div>";
  $html = $html."<div class='woogie-content-center-reply'>".
				"<textarea id='textarea' rows='4' cols='50'></textarea>".
				"<button onclick='sendMessage($cid, $numberSend)'>Send</button>".
				"</div>";

  $html = $html . "%#%<div class='woogie-profile-pic'>".
		"<img src='images/user_2.png'/>".
		"</div>".
		"<p class='p-name'>$cname</p>".
		"<p class='p-number'>$cnumber</p>".
		"<a href='mailto:$cemail'>$cemail</a>";

 echo $html; 
mysqli_close($con);
?>
